//
//  VideoVC.m
//  Project2
//
//  Created by hundredsandc on 2017/11/4.
//  Copyright © 2017年 hundredsandc. All rights reserved.
//

#import "VideoVC.h"
#import "HomeCollectionReusableView.h"
#import "VideoHeaderCell.h"
#import "VideoCycleView.h"
#import "HomeCollectionViewCell.h"
#import "collectionFooter.h"
#import "JiJinModel.h"
#import "WEBViewController.h"
#import "MoreVideoVC.h"

@interface VideoVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,ZXRollViewDelegate>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)UICollectionViewFlowLayout *flowLayOut;
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic,strong)NSMutableArray *videoArr;
@property (nonatomic,strong)NSMutableArray *data4Arr;
@property (nonatomic,strong)NSMutableArray *data8Arr;
@property (nonatomic, strong) ZXRollView *rollView;
@end

@implementation VideoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self getData];
    [self getDataType4];
    [self getDataType8];
}
-(void)getDataType4{
    
    //    http://cdn.a8.tvesou.com/right/hot_video_list?
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    //全部：0，NBA：1，英超：4，CBA：2，西甲：6，中超：9，德甲：3，意甲：5， 欧冠：8，法甲：7
    //    http://cdn.a8.tvesou.com/right/recommend_video_list?leagueId=4&page=0&pageSize=20
    params[@"leagueId"] = @(4);
    params[@"page"] = @(0);
    params[@"pageSize"] = @(20);
    [HTTPManagers getWithLiveURL:@"recommend_video_list" params:params success:^(id json) {
        NSArray *arr= [json objectForKey:@"data"];
        if (self.data4Arr.count>0) {
            [_data4Arr removeAllObjects];
        }
        for (int i = 0; i<arr.count; i++) {
            NSDictionary *dict = arr[i];
            JiJinModel *model = [JiJinModel mj_objectWithKeyValues:dict];
            [_data4Arr addObject:model];
            
        }
        [_collectionView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}
-(void)getDataType8{
    
    //    http://cdn.a8.tvesou.com/right/hot_video_list?
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    //全部：0，NBA：1，英超：4，CBA：2，西甲：6，中超：9，德甲：3，意甲：5， 欧冠：8，法甲：7
    //    http://cdn.a8.tvesou.com/right/recommend_video_list?leagueId=4&page=0&pageSize=20
    params[@"leagueId"] = @(8);
    params[@"page"] = @(0);
    params[@"pageSize"] = @(20);
    [HTTPManagers getWithLiveURL:@"recommend_video_list" params:params success:^(id json) {
        NSArray *arr= [json objectForKey:@"data"];
        if (self.data8Arr.count>0) {
            [_data8Arr removeAllObjects];
        }
        for (int i = 0; i<arr.count; i++) {
            NSDictionary *dict = arr[i];
            JiJinModel *model = [JiJinModel mj_objectWithKeyValues:dict];
            [_data8Arr addObject:model];
            
        }
        [_collectionView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}
-(void)getData{
    
    //    http://cdn.a8.tvesou.com/right/hot_video_list?
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    //全部：0，NBA：1，英超：4，CBA：2，西甲：6，中超：9，德甲：3，意甲：5， 欧冠：8，法甲：7
    
    [HTTPManagers getWithLiveURL:@"hot_video_list" params:params success:^(id json) {
        [_collectionView.mj_header endRefreshing];
        NSArray *arr= [json objectForKey:@"data"];
        if (self.videoArr.count>0) {
            [_videoArr removeAllObjects];
        }
        for (int i = 0; i<arr.count; i++) {
            NSDictionary *dict = arr[i];
            JiJinModel *model = [JiJinModel mj_objectWithKeyValues:dict];
            [_videoArr addObject:model];
            
        }
        [_collectionView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}
-(void)setupUI{
    [self.view addSubview:self.collectionView];
    [self addConstraint];
}
#pragma mark - constraint
-(void)addConstraint{
    WS(ws);
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.view);
        make.bottom.mas_equalTo(ws.view).with.offset(-64);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.left.equalTo(ws.view);
    }];
    
}

#pragma mark - collection
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    int i =2;
    return i+1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section==0) {
        return 1;
    }
    return 2;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        VideoHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([VideoHeaderCell class]) forIndexPath:indexPath];
        
        _rollView = [[ZXRollView alloc] initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,170)];

        self.rollView.delegate = self;
        self.rollView.autoRolling = YES;
        self.rollView.hideIndicatorForSinglePage = YES;
        
        self.rollView.interitemSpacing = 0;
        [self.rollView reloadViews];
        
        [cell.contentView addSubview:self.rollView];

        return cell;
    }else if(indexPath.section==1){
        HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomeCollectionViewCell class]) forIndexPath:indexPath];
        
        if (_data4Arr.count>0) {
            
        JiJinModel *model = self.data4Arr[indexPath.row];
        NSString *imageUrlStr = [[model.videos firstObject]objectForKey:@"thumb"];
        
       
        
        
        [cell customImageUrl:imageUrlStr time:model.source title:model.title type:nil];
        }
        return cell;
    }else{
        HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomeCollectionViewCell class]) forIndexPath:indexPath];
        
        if (_data8Arr.count>0) {
            
            JiJinModel *model = self.data8Arr[indexPath.row];
            NSString *imageUrlStr = [[model.videos firstObject]objectForKey:@"thumb"];
            
            
            
            
            [cell customImageUrl:imageUrlStr time:model.source title:model.title type:nil];
        }
        return cell;

    }
}

#pragma mark <ZXRollViewDelegate>
- (NSInteger)numberOfItemsInRollView:(ZXRollView *)rollView {
    return 3;
}

- (void)rollView:(nonnull ZXRollView *)rollView setViewForRollView:(nonnull UIView *)view atIndex:(NSInteger)index {
    switch (index) {
        case 0: {
            if (self.videoArr.count>0) {
                
            JiJinModel *model = self.videoArr[index];
            NSString *imageUrlStr = [[model.videos firstObject]objectForKey:@"thumb"];
           
            
            VideoCycleView *cycle = [[VideoCycleView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 170)];
            [cycle customImageUrlStr:imageUrlStr title:model.title time:model.source];
            [view addSubview:cycle];
            }
            
        }
            break;
        case 1:
        {
            if (self.videoArr.count>0) {
                
                JiJinModel *model = self.videoArr[index];
                NSString *imageUrlStr = [[model.videos firstObject]objectForKey:@"thumb"];
                
                
                VideoCycleView *cycle = [[VideoCycleView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 170)];
                [cycle customImageUrlStr:imageUrlStr title:model.title time:model.source];
                [view addSubview:cycle];
            }
           
        }
            break;
        case 2:
        {
            if (self.videoArr.count>0) {
                
                JiJinModel *model = self.videoArr[index];
                NSString *imageUrlStr = [[model.videos firstObject]objectForKey:@"thumb"];
                
                
                VideoCycleView *cycle = [[VideoCycleView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 170)];
                [cycle customImageUrlStr:imageUrlStr title:model.title time:model.source];
                [view addSubview:cycle];
            }
        }
            
            break;
        default:
            break;
    }
}

- (void)rollView:(ZXRollView *)rollView didTapItemAtIndex:(NSInteger)index {
    JiJinModel *model = self.videoArr[index];
    NSString *url = [[model.videos firstObject]objectForKey:@"url"];
    WEBViewController *web = [[WEBViewController alloc]init];
    web.titleStr = @"视屏";
    web.url = url;
    [self.navigationController pushViewController:web animated:YES];
}



-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind ==UICollectionElementKindSectionHeader) {
        
        HomeCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([HomeCollectionReusableView class]) forIndexPath:indexPath];
        reusableView.titleLabel.text = @"英超比赛集锦";
        if (indexPath.section==2) {
            reusableView.titleLabel.text = @"欧冠比赛集锦";
        }
        reusableView.subTitleLabel.text = @"更多";
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(moreVideoClick:)];
        [reusableView addGestureRecognizer:tap];
        return reusableView;
    }else{
        collectionFooter *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([collectionFooter class]) forIndexPath:indexPath];
        footer.backgroundColor = RGBCOLOR(0xf4, 0xf4, 0xf4);
        return footer;
    }
}
-(void)moreVideoClick:(UIGestureRecognizer *)gr{

//    视屏播放
    MoreVideoVC *vc = [MoreVideoVC new];
     //    0 全部 1nba 2cba 3德甲 4英超 5意甲 6西甲 7法甲 8欧冠 9中超
    vc.dataArr = @[@"全部",@"NBA",@"CBA",@"德甲",@"英超",@"意甲",@"西甲",@"法甲",@"欧冠",@"中超"];
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return CGSizeMake(0, 0);
    }
    return CGSizeMake(SCREEN_WIDTH, 40);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (section==0) {
        return CGSizeMake(0, 0);
    }
    return CGSizeMake(SCREEN_WIDTH, 10);

}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (section==0) {
        
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }else{
     return UIEdgeInsetsMake(10, 15, 0, 15);
    }
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        
        return CGSizeMake(SCREEN_WIDTH, 170);
    }else{
        return CGSizeMake((SCREEN_WIDTH-39)/2, 137+10);
    }
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (indexPath.section==1) {
        
        
        JiJinModel *model = self.data4Arr[indexPath.row];
        NSString *url = [[model.videos firstObject]objectForKey:@"url"];
        WEBViewController *web = [[WEBViewController alloc]init];
        web.titleStr = @"视屏";
        web.url = url;
        [self.navigationController pushViewController:web animated:YES];

    }else if (indexPath.section==2){
    
        JiJinModel *model = self.data8Arr[indexPath.row];
        NSString *url = [[model.videos firstObject]objectForKey:@"url"];
        WEBViewController *web = [[WEBViewController alloc]init];
        web.titleStr = @"视屏";
        web.url = url;
        [self.navigationController pushViewController:web animated:YES];
    }
}
-(UICollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayOut];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:([HomeCollectionReusableView class]) forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([HomeCollectionReusableView class])];
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([VideoHeaderCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([VideoHeaderCell class])];
        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([HomeCollectionViewCell class])];
        [_collectionView registerClass:([collectionFooter class]) forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([collectionFooter class])];

        
    }
    return _collectionView;
}
-(UICollectionViewFlowLayout *)flowLayOut{
    if (!_flowLayOut) {
        _flowLayOut = [[UICollectionViewFlowLayout alloc] init];
        
        _flowLayOut.minimumInteritemSpacing = 9;
        _flowLayOut.minimumLineSpacing = 15;
        [_flowLayOut setScrollDirection:UICollectionViewScrollDirectionVertical];
        
    }
    return _flowLayOut;
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
-(NSMutableArray *)videoArr{
    if (!_videoArr) {
        _videoArr = [NSMutableArray array];
    }
    return _videoArr;
}
-(NSMutableArray *)data4Arr{
    if (!_data4Arr) {
        _data4Arr = [NSMutableArray array];
    }
    return _data4Arr;
}
-(NSMutableArray *)data8Arr{
    if (!_data8Arr) {
        _data8Arr = [NSMutableArray array];
    }
    return _data8Arr;
}

@end
